package com.ytclub.auth.domain.service.impl;

import com.alibaba.fastjson.JSON;
import com.ytclub.auth.common.enums.IsDeletedFlagEnum;
import com.ytclub.auth.domain.convent.AuthRolePermissionBOConverter;
import com.ytclub.auth.domain.entity.AuthRolePermissionBO;
import com.ytclub.auth.domain.service.AuthRolePermissionDoaminService;
import com.ytclub.auth.infra.basic.entity.AuthRolePermission;
import com.ytclub.auth.infra.basic.service.AuthRolePermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * 角色权限领域层接口实现类
 *
 * @author: HuangQi
 * @date: Created in 21:14 2023/12/19
 * @description: 角色权限领域层接口实现类
 */
@Service
@Slf4j
public class AuthRolePermissionDoaminServiceImpl implements AuthRolePermissionDoaminService {


    @Resource
    private AuthRolePermissionService authRolePermissionService;


    /**
     * 新增角色
     *
     * @param authRolePermissionBO 新增角色的信息
     * @return 新增结果
     */
    @Override
    public Boolean add(AuthRolePermissionBO authRolePermissionBO) {
        if (log.isInfoEnabled()) {
            log.info("AuthRolePermissionDoaminServiceImpl.add.bo:{}", JSON.toJSONString(authRolePermissionBO));
        }

        // 参数转换
        List<AuthRolePermission> rolePermissionList = new LinkedList<>();
        Long roleId = authRolePermissionBO.getRoleId();
        authRolePermissionBO.getPermissionIdList().forEach(permissionId -> {
            AuthRolePermission authRolePermission = new AuthRolePermission();
            authRolePermission.setRoleId(roleId);
            authRolePermission.setPermissionId(permissionId);
            authRolePermission.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
            rolePermissionList.add(authRolePermission);
        });

        // 调用service
        int count = authRolePermissionService.batchInsert(rolePermissionList);

        // 返回结果
        return count > 0;
    }


}
